package extra;

import psl.AppConstants;

import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: phiteshkumar
 * Date: Feb 24, 2007
 * Time: 10:25:01 AM
 */
public class HTMLServices
{
    public static String getPageSelection(int fiPages, int fiCurrentPage, int fiPageSize, int fiRows) throws IOException
    {

        System.out.println ("In The HTMLServices.java ");

        StringBuffer foOut = new StringBuffer();
        if (fiRows == 0) return "";
        if (fiPages <= 1) return "";

        int liRangeBegin = 1;
        int liRangeEnd = fiPages;

        if (fiPages > 30)
        {
            int liTemp = (fiCurrentPage / 10) * 10;
            if ((fiCurrentPage - liTemp) >= 5) liTemp += 10;
            liRangeBegin = liTemp - 9;
            if (liRangeBegin < 1) liRangeBegin = 1;
            liRangeEnd = liRangeBegin + 19;
            if (liRangeEnd > fiPages) liRangeEnd = fiPages;
        }

        foOut.append("<table width=98% class='itemNavigation'><tr>\r\n");
        foOut.append("<td accesskey='0'>\r\n");
        foOut.append("Page:\r\n");

        int liPage;
        if (fiCurrentPage > 1)
        {
            liPage = fiCurrentPage - 1;
            foOut.append("<a href='javascript:scrollToPage(1);'><img align='middle' border=0 alt='First' src='" + AppConstants.IMAGES + "/FirstPage.gif'></a>\r\n");
            foOut.append("<a href='javascript:scrollToPage(" + liPage + ");'><img align='middle' border=0 alt='Prior' src='" + AppConstants.IMAGES + "/PreviousPage.gif'></a>\r\n");
        }

        liPage = 1;
        while (true)
        {
            if (liPage == fiCurrentPage)
            {
                foOut.append("<b>" + fiCurrentPage + "</b>\r\n");
            }
            else
            {
                foOut.append("<a href='javascript:scrollToPage(" + liPage + ");' accesskey='"+liPage+"' >" + liPage + "</a>\r\n");
            }

            if (liPage >= fiPages) break;

            if (liRangeBegin <= liPage && liPage <= liRangeEnd)
            {
                liPage++;
            }
            else
            {
                liPage += 10;
                if (liPage > fiPages) liPage = fiPages;
            }
        }

        if (fiCurrentPage < fiPages)
        {
            liPage = fiCurrentPage + 1;
            foOut.append("<a href='javascript:scrollToPage("+liPage+");'><img align='middle' border=0 alt='Next' src='" + AppConstants.IMAGES + "/NextPage.gif'></a>\r\n");
            foOut.append("<a href='javascript:scrollToPage("+fiPages+");'><img align='middle' border=0 alt='Last' src='" + AppConstants.IMAGES + "/LastPage.gif'></a>\r\n");
        }
        int liStartItem = ((fiCurrentPage - 1) * fiPageSize) + 1;
        int liEndItem = liStartItem + fiPageSize - 1;
        if (liEndItem > fiRows) liEndItem = fiRows;
        foOut.append("</td>\r\n");
        foOut.append("<td align='right'>Items " + liStartItem + "-" + liEndItem + " of " + fiRows + "</td>\r\n");
        foOut.append("</tr></table>\r\n");
        return foOut.toString();
    }
    
}
